#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "database.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
#include "halldevice.h"
#include "collectingbox.h"
#include <QTimer>
#include <QJsonArray>
#include <QJsonObject>
#include <currentsource.h>
typedef struct ProcessReturnStruct
{
    bool isSuccess=false;
    int step=1;
    QVariant value=0;

}ReturnStruct;

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    HallDevice hallDevice[5];
    CollectingBox box[5];
    DataBase db;
    void SlotInit();
    void InitComboBox();

    QStringList m_SWAP;
    QStringList m_G0;
    QStringList m_G1;
    QStringList m_G2;
    QStringList m_EXC;
    QStringList m_VEXC;
    QStringList m_VBIAS;
    QStringList m_Bandwidth;

    void InitPortCanUse();
    QByteArray PackageSendData(int messageType);
    QByteArray RecvProgrammingDeviceData(HallDevice* device);
    int nowStep=0;
    int toStep=0;
    QTimer ProcessEngine;
    ReturnStruct returnValue[5];

    bool isShowedMessageBox=false;

    QJsonArray  HallArray;
    QStringList WriteFlash;
    int         HallIndex = 0;                          //霍尔选型
    float       Get_Voltage     = 0;

    CurrentSource currentSource;
private:
    Ui::MainWindow *ui;
public slots:
    void ShowMainWidget();
    void ShowComSetWidget();
    void ShowParamSetWidget();

    void Slot_OpenCom1Device();
    void Slot_CloseCom1Device();

    void SendTestData();
    void SwitchToReadWriteMode();
    void SwitchToMeasureMode();
    void WriteZeroDacData();

    //可编程转接采集盒1（万用表）
    void Slot_OpenBoxDevice1Com();              //打开box1的串口
    void Slot_ShowOpenComWidget(QString);       //box1串口打开的提示信息
    void Slot_CloseBoxDevice1Com();             //关闭Box1打开的串口
    void Slot_Box1ReadVoltage();                //box1读取串口电压
    //可编程转接采集盒2（万用表）
    void Slot_OpenBoxDevice2Com();              //打开box2的串口
    void Slot_CloseBoxDevice2Com();             //关闭Box2打开的串口
    void Slot_Box2ReadVoltage();                //box2读取串口电压
    //可编程转接采集盒3（万用表）
    void Slot_OpenBoxDevice3Com();              //打开box3的串口
    void Slot_CloseBoxDevice3Com();             //关闭Box3打开的串口
    void Slot_Box3ReadVoltage();                //box3读取串口电压
    //可编程转接采集盒4（万用表）
    void Slot_OpenBoxDevice4Com();              //打开box4的串口
    void Slot_CloseBoxDevice4Com();             //关闭Box4打开的串口
    void Slot_Box4ReadVoltage();                //box4读取串口电压
    //可编程转接采集盒5（万用表）
    void Slot_OpenBoxDevice5Com();              //打开box5的串口
    void Slot_CloseBoxDevice5Com();             //关闭Box5打开的串口
    void Slot_Box5ReadVoltage();                //box5读取串口电压
    //电流源
    void Slot_OpenCurrentSource();
    void Slot_CloseCurrentSource();
    void Slot_ShowCurrentSourceWidget();        //显示电流源界面
    void Slot_0A();
    void Slot_50A();
    void Slot_ShowOpenCurrent(QString);
    //*************************************************
    void ReadConfigFile();
    void PharseConfigFile();
    void GetWriteInBufferValue();

    int GetNextStep();        //执行完动作后，获取下一个步骤，如果都失败，则停止

    void Slot_AutoTest();
    void Slot_StartAutoTest();

    ReturnStruct PowerOnChip(int i);
    ReturnStruct ReadTADCFuntion(int i);
    ReturnStruct ReadBurnTime(int i);
    ReturnStruct StepChipToWriteMode(int i);
    ReturnStruct ChipWriteTC1(int i);
    ReturnStruct ChipWriteTC2(int i);
    ReturnStruct StartChipCalibration(int i);
    ReturnStruct ChipWriteBurnsNumber(int i);
    ReturnStruct ChipWriteVBIAS(int i);
    ReturnStruct ChipWriteVEXC(int i);
    ReturnStruct ChipWriteEnlarge(int i);
    ReturnStruct ChipWriteZeroL(int i);
    ReturnStruct ChipWriteZeroH(int i);
    ReturnStruct ChipWriteGainL(int i);
    ReturnStruct ChipWriteGainH(int i);
    ReturnStruct OutSiganlPowerON();
    ReturnStruct SourcePowerOn();
    void TransmitStep(bool);
    void TransmitStep(bool bValue,int step);
    ReturnStruct ChipCaptureVoltage(int i);
    ReturnStruct ChipGetGain(int i);
    void GetGain_enumG3(float ActualRange,int i);
    ReturnStruct ChipRoughToWrite(int i);
    ReturnStruct ChipRoughWriteZeroL(int i);
    ReturnStruct ChipRoughWriteZeroH(int i);
    ReturnStruct ChipRoughWriteGainL(int i);
    ReturnStruct ChipRoughWriteGainH(int i);
    ReturnStruct ChipCaptureAfterRoughVoltage(int i);
    ReturnStruct OutSiganlPowerOff();
    ReturnStruct SourcePowerOff();
    ReturnStruct ChipCaptureAfterPoweroffVoltage(int i);
    ReturnStruct ChipGetUnError(int i);                 //获得量程误差
    ReturnStruct ChipFineGetGain(int i);
    bool setGainZero(float DifferenceValue,int mode,int i);
    ReturnStruct ChipFineWriteGainL(int i);
    ReturnStruct ChipFineWriteGainH(int i);
    ReturnStruct NormalGetVoltage(int i);
    ReturnStruct ChipFineGetZeroError(int i);
    ReturnStruct ChipFineGetZero(int i);
    ReturnStruct ChipFineWriteZeroL(int i);
    ReturnStruct ChipFineWriteZeroH(int i);
    ReturnStruct ChipFineAloneZero1(int i);
    ReturnStruct ChipFineAloneZero3(int i);
    ReturnStruct ChipFineAloneZero2(int i);

    void HandleWrite();

    void AddProductModel();
    QJsonObject AddJsonObj(QString ObjName);
    QString CreateJsonObj(QString ObjName);
    void WriteJson(QString HallName);
    void SaveHallConfig();
    void LoadHallConfig();

    void Slot_ShowString(QString recv);
    void BurnAll();
    void LoadCalibrationData(int i);
};
#endif // MAINWINDOW_H
